package com.sxt.bus.controller;

import com.sxt.bus.service.MobilesService;
import com.sxt.bus.utils.MobileNumberUtils;
import com.sxt.bus.vo.MobilesVo;
import com.sxt.sys.constant.SYSConstant;
import com.sxt.sys.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 手机号控制器
 * @author WanMing
 * @date 2019/2/8 11:33
 */

@Controller
@RequestMapping("mobile")
public class MobileController {

    @Autowired
    private MobilesService mobilesService;

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("toAddMobile")
    public String toAddView(){
        return "business/addMobile";
    }

    /**
     * 添加号段
     * @param mobilesVo
     * @return
     */
    @RequestMapping("addMobile")
    @ResponseBody
    public ResultObj addMobile(MobilesVo mobilesVo){
        ResultObj resultObj = null;
        //获得连续号段
        List<String> mobileNumber = MobileNumberUtils.createMobileNumber(mobilesVo.getStrNumber(), mobilesVo.getEndNumber());

        try {
            mobilesService.addMobiles(mobileNumber,mobilesVo.getMobileType());
            resultObj = new ResultObj(SYSConstant.SUCCESS_CODE,SYSConstant.ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = new ResultObj(SYSConstant.ERROR_CODE,SYSConstant.ADD_ERROR);

        }

        return resultObj;

    }
}
